package com.hue.controller;

import com.hue.entity.User;
import com.hue.entity.vo.GoodsVo;
import com.hue.service.GoodsService;
import com.hue.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private ThymeleafViewResolver thymeleafViewResolver;
    @Autowired
    private RedisTemplate redisTemplate;


    /*
        将页面做缓存:
            QPS:
                5000个线程:592.7

     */
    @GetMapping(value = "/toList", produces = "text/html;charset=utf-8")    //设置返回类型为html
    @ResponseBody
    public String toList(Model model, HttpServletRequest request, HttpServletResponse response) {
        //判断页面是否存在
        ValueOperations operations = redisTemplate.opsForValue();
        Object goodsListHtml = operations.get("goodsListHtml");
        if (goodsListHtml == null) {    //没有缓存
            //查询数据库
            List<GoodsVo> goodsVoList = goodsService.getAllGoodsVo();
            model.addAttribute("goodsVoList", goodsVoList);
            //手动渲染页面
            WebContext webContext = new WebContext(request, response, request.getServletContext(),
                    request.getLocale(), model.asMap());
            goodsListHtml = thymeleafViewResolver.getTemplateEngine().process("goodsList", webContext);
            //将页面加入到缓存,设置过期时间为1分钟
            operations.set("goodsListHtml", goodsListHtml, 60, TimeUnit.SECONDS);
        }
        return (String) goodsListHtml;
    }

    /*
        不做页面缓存：
            QPS:
                5000个线程:358.5
     */
    @GetMapping(value = "/toList1")
    public String toList1(Model model){
        List<GoodsVo> goodsVoList = goodsService.getAllGoodsVo();
        model.addAttribute("goodsVoList", goodsVoList);
        return "goodsList";
    }


    /*
        将页面做URL缓存
     */
    @GetMapping(value = "/goodsDetail", produces = "text/html;charset=utf-8")
    @ResponseBody
    public String goodsDetail(Integer goodsId, Model model, @CookieValue("token") String token
            , HttpServletRequest request, HttpServletResponse response) {
        //判断页面是否存在
        ValueOperations operations = redisTemplate.opsForValue();
        Object goodsDetailHtml = operations.get("goodsDetailHtml:" + goodsId);
        if (goodsDetailHtml == null) {  //没有缓存
            //查询数据库
            GoodsVo goods = goodsService.getGoodsById(goodsId);
            model.addAttribute("goods", goods);
            User user = redisUtils.getUser(token);
            model.addAttribute("user", user);
            Date startDate = goods.getStartDate();
            Date endDate = goods.getEndDate();
            Date now = new Date();
            int secKillStatus;
            int remainSeconds;
            if (now.before(startDate)) {
                secKillStatus = 0;
                remainSeconds = (int) ((startDate.getTime() - now.getTime()) / 1000);
            } else if (now.after(endDate)) {
                secKillStatus = 2;
                remainSeconds = -1;
            } else {
                secKillStatus = 1;
                remainSeconds = 0;
            }
            model.addAttribute("secKillStatus", secKillStatus);
            model.addAttribute("remainSeconds", remainSeconds);
            //手动渲染页面
            WebContext webContext = new WebContext(request, response, request.getServletContext(),
                    request.getLocale(), model.asMap());
            goodsDetailHtml = thymeleafViewResolver.getTemplateEngine().process("goodsDetail", webContext);
            operations.set("goodsDetailHtml:" + goodsId, goodsDetailHtml, 60, TimeUnit.SECONDS);
        }
        return (String) goodsDetailHtml;
    }
}
